/**
 * Created with IntelliJ IDEA
 * Description:
 * User: Administrator
 * Data: 2023 - 08 - 08
 * Time: 14:26
 */




import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;



import javax.sql.DataSource;

import java.sql.*;

import java.text.ParseException;

import java.text.SimpleDateFormat;



class Util {



    //使用连接池

    private static final DataSource DATA_SOURCE    = new MysqlDataSource();



    static {

        ((MysqlDataSource) DATA_SOURCE).setUrl("jdbc:mysql://127.0.0.1:3306/ebook?characterEncoding=utf8&useSSL=false");

        ((MysqlDataSource) DATA_SOURCE).setUser("root");

        ((MysqlDataSource) DATA_SOURCE).setPassword("111111");

    }



    public static void main(String[] args) {

        System.out.println(getConnection());

    }

    // 获取数据库连接

    public static Connection getConnection(){

        try {

            return DATA_SOURCE.getConnection();

        } catch (SQLException e) {

            throw new RuntimeException("获取数据库连接失败", e);

        }

    }

    // 释放资源

    public static void close(ResultSet resultSet, Statement statement,

                             Connection connection){



        try {

            if(resultSet != null) {

                resultSet.close();

            }

            if(statement != null){

                statement.close();

            }

            if(connection != null){

                connection.close();

            }

        } catch (SQLException e) {

            throw new RuntimeException("数据库操作异常", e);

        }

    }

    //日期字符串转Java日期类Date和sql时间戳Timestamp

    public static Timestamp getTimestamp(String dateString){

        try {

            // 年-月-日 小时:分钟:秒

            java.util.Date date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(dateString);

            return new java.sql.Timestamp(date.getTime());

        } catch (ParseException e) {

            throw new RuntimeException("日期格式化错误："+dateString, e);

        }

    }

}





class AddBorrow {



    /**

     * 新增貂蝉同学的借阅记录：诗经，从2019年9月25日17:50到2019年10月25日17:50

     */

    public static void main(String[] args) {

        //1.创建数据库连接对象

        Connection connection = null;

        //2.创建操作命令对象

        PreparedStatement preparedStatement = null;

        try {

            connection = Util.getConnection();

            String sql = "insert into borrow_info(book_id, student_id," +

                    " start_time, end_time) select b.id,s.id,?,?" +

                    " from book b,student s where b.name=? and s.name=?";

            preparedStatement = connection.prepareStatement(sql);

            preparedStatement.setTimestamp(1, Util.getTimestamp("2019-09-25 17:50:00"));

            preparedStatement.setTimestamp(2, Util.getTimestamp("2019-10-25 17:50:00"));

            preparedStatement.setString(3, "诗经");

            preparedStatement.setString(4, "貂蝉");

            System.out.println(preparedStatement);

            //3.执行sql

            int result = preparedStatement.executeUpdate();

            System.out.println(result);

        } catch (SQLException e) {

            e.printStackTrace();

        } finally {

            //4.释放资源

            Util.close(null, preparedStatement, connection);

        }

    }

}



